package com.wjd.wshow.service.impl;

import com.wjd.wshow.common.resource.Resource;
import com.wjd.wshow.common.resource.ResourceType;
import com.wjd.wshow.entity.Video;
import com.wjd.wshow.common.resource.entity.VideoResource;
import com.wjd.wshow.repository.VideoRepository;
import com.wjd.wshow.service.CatalogService;
import com.wjd.wshow.service.ThumbnailService;
import com.wjd.wshow.service.VideoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service("VideoService")
public class VideoServiceImpl extends ResourceServiceImpl<Video> implements VideoService {

    private static Logger logger = LoggerFactory.getLogger(VideoServiceImpl.class);

    @javax.annotation.Resource(name = "CatalogService")
    private CatalogService catalogService;

    @javax.annotation.Resource(name = "ThumbnailService")
    private ThumbnailService thumbnailService;

    @javax.annotation.Resource(name = "VideoRepository")
    private VideoRepository videoRepository;

    @Override
    protected ResourceType getResourceType() {
        return ResourceType.VIDEO;
    }

    @Override
    public String addVideo(Video video) {
        Video v = create(video);
        if (v.getId() == null) {
            logger.error("Add video failed!");
            return null;
        }
        VideoResource resource = (VideoResource) getResource(v.getId());
        if (resource != null) {
            thumbnailService.createThumbnail(resource);
        }
        logger.info("Add video: " + v.getId());
        return v.getId();
    }

    @Override
    public boolean deleteVideo(String id) {
        // 删除缩略图
        thumbnailService.deleteThumbnail(id);
        deleteById(id);
        logger.info("Delete video: " + id);
        return true;
    }

    @Override
    public boolean deleteAllVideos() {
        List<Video> videos = getVideos();
        for (Video v : videos) {
            deleteVideo(v.getId());
        }
        return true;
    }

    @Override
    public boolean updateVideo(Video video) {
        if (!getRepository().existsById(video.getId())) {
            logger.info("Update video not exist: " + video.getId());
            return false;
        }
        update(video);
        logger.info("Update video: " + video.getId());
        return true;
    }

    @Override
    public Optional<Video> getVideo(String id) {
        return getRepository().findById(id);
    }

    @Override
    public List<Video> getVideos() {
        List<Video> videos = new ArrayList<>();
        Iterable<Video> it = getRepository().findAll();
        for (Video v : it) {
            videos.add(v);
        }
        return videos;
    }

    @Override
    public List<Video> findVideos(String path) {
        if (path == null) {
            return new ArrayList<>(0);
        }
        return getRepository().findByPath(path);
    }

    @Override
    public Resource getResource(String id) {
        VideoResource resource;
        Optional<Video> optionalVideo = getVideo(id);
        if (!optionalVideo.isPresent()) {
            return null;
        }
        resource = new VideoResource(optionalVideo.get());
        return resource;
    }

    @Override
    public void deleteResource(String id) {
        thumbnailService.deleteThumbnail(id);
        super.deleteResource(id);
    }

    @Override
    protected VideoRepository getRepository() {
        return videoRepository;
    }

    @Override
    protected CatalogService getCatalogService() {
        return catalogService;
    }
}
